/**
 * Created by waterworld on 16-9-26.
 */
var P = require("bluebird");
var cnst=require('../../../shared/const');
var wilddogPage = module.exports;
var countPerPage=cnst.countPerPage;
wilddogPage.setInstance=function(wilddog)
{    this.wilddog=wilddog;
};

//pageInfo {totalPage:0,pageStart:0,lastPageIndex:0,lastRecordPos:0};
//msg {img:xxx,txt:xxx,time:xxxxx}
wilddogPage.appendBlog= P.coroutine(function*(path,topic,pageInfo,msg)
{
    var pageNo=Math.floor(pageInfo.lastRecordPos/countPerPage);
    var finalPath=path+"/"+topic+"/p"+pageNo;//blog/12/第一篇日志/
    var  data={};
    var  k='i'+pageInfo.lastRecordPos;
    data[k]=msg;
    pageInfo.lastRecordPos++;
    msg.star=0;
    msg.comment=0;
    yield this.update(finalPath,data);
    finalPath=path+'/'+topic+'/pageInfo';
    yield this.update(finalPath,pageInfo);
});

//pageInfo {totalPage:0,pageStart:0,lastPageIndex:0,lastRecordPos:0};
//msg {img:xxx,txt:xxx,time:xxxxx}
wilddogPage.appendComment= P.coroutine(function*(path,topic,pageInfo,msg)
{
    var pageNo=Math.floor(pageInfo.lastRecordPos/countPerPage);
    var finalPath=path+"/"+topic+"/p"+pageNo;//blog/12/空间/p1/i11/留言/p0/i0
    var  data={};
    var  k='i'+pageInfo.lastRecordPos;
    data[k]=msg;
    pageInfo.lastRecordPos++;
    yield this.update(finalPath,data);
    finalPath=path+'/'+topic+'/pageInfo';
    yield this.update(finalPath,pageInfo);
    return pageInfo
});
//blog/12/空间/p1/i11/留言/p0/i0
wilddogPage.addComment= P.coroutine(function*(uid,blogIndex,pageInfo,msg)
{
    var p1=Math.floor(blogIndex/countPerPage);
    var p2=Math.floor(pageInfo.lastRecordPos/countPerPage);
    var finalPath="cmt/"+uid+'/i'+blogIndex+'/p'+p2//comment/12/b11/p0
    var  data={};
    var  k='i'+pageInfo.lastRecordPos;
    data[k]=msg;
    pageInfo.lastRecordPos++;
    yield this.update(finalPath,data);
    finalPath="cmt/"+uid+'/i'+blogIndex+'/pageInfo';
    yield this.update(finalPath,pageInfo);
    var path="blog/"+uid+"/空间/p"+p1+'/i'+blogIndex+'/comment';
    var ref=this.wilddog.sync().ref(path);
    ref.transaction(function(value)
    {if(value==null)
          return 0;// 第一次获取为null，返回一个默认值
            return pageInfo.lastRecordPos;
    });
    return pageInfo
});

wilddogPage.getCommentPage= P.coroutine(function*(uid,blogIndex,pageIndex)
{
    var finalPath="cmt/"+uid+'/i'+blogIndex+'/p'+pageIndex;
    var page=yield this.getDataAsync(finalPath);
    return page;
});

wilddogPage.getLastCommentPage= P.coroutine(function*(uid,blogIndex,pageInfo)
{
    var finalPath='cmt/'+uid+'/i'+blogIndex+'/pageInfo';
    var pageNo=Math.ceil(pageInfo.lastRecordPos/(countPerPage))-1;
    if(pageNo<0) pageNo=0;
    finalPath='cmt/'+uid+'/i'+blogIndex+"/p"+pageNo;
    var page=yield this.getDataAsync(finalPath);
    return page;
});


wilddogPage.getPage= P.coroutine(function*(path,topic,pageIndex)
{
    var finalPath=path+"/"+topic+"/p"+pageIndex;
    var page=yield this.getDataAsync(finalPath);
    return page;
});

wilddogPage.getNewlyPage= P.coroutine(function*(path,topic)
{
    var finalPath=path+"/"+topic+"/pageInfo";
    var pageInfo=yield this.getDataAsync(finalPath);
    if(!pageInfo)
    { return null;}
    var pageNo=Math.ceil(pageInfo.lastRecordPos/(countPerPage))-1;
    finalPath=path+"/"+topic+"/p"+pageNo;
    var page=yield this.getDataAsync(finalPath);
    return page;
});

//记录总的页面数目，起始页面index，
wilddogPage.getPageInfo= P.coroutine(function*(path,topic)
{
    var finalPath=path+'/'+topic+'/pageInfo';
    var pageInfo=yield this.getDataAsync(finalPath);
    if(!pageInfo)
    {
        pageInfo={totalPage:0,pageStart:0,lastPageIndex:0,lastRecordPos:0};
        yield this.update(finalPath,pageInfo);
    }
    pageInfo.countPerPage=cnst.countPerPage;
    return pageInfo;
});

wilddogPage.deleteComment= P.coroutine(function*(path,topic,recordIndex)
{
    var pageNo=Math.floor(recordIndex/countPerPage);
    var finalPath=path+"/"+topic+"/p"+pageNo+'/i'+recordIndex;
    var ref=this.wilddog.sync().ref(finalPath);
    ref.remove();
});



wilddogPage.getDataAsync= P.coroutine(function*(path)
{
    if(!this.wilddog)
    { return null;
    }
    var ref=this.wilddog.sync().ref(path);
    var getDataAsync=new Promise(function(resolveFuncRet, rejectErr) {ref.once("value", resolveFuncRet);});
    var data=yield getDataAsync.then(function(data){ return data;});
    return data.val();
});


wilddogPage.update= P.coroutine(function*(path,value){
    var ref = this.wilddog.sync().ref(path);//获取节点
    var updateAsync=new Promise(function(resolveFuncRet, rejectErr) {ref.update(value, resolveFuncRet);});
    try
    {   var ret= yield updateAsync.then(function(err){ if(err==null) return true;else return false;});
        return ret;
    }
    catch(e)
    { return false;
    }
});
